package com.xtjc.recsyslog.netty.command;

import cn.hutool.core.util.ArrayUtil;
import com.xtjc.recsyslog.netty.entity.ChargeEntity;
import com.xtjc.recsyslog.util.ConverUtil;
import com.xtjc.recsyslog.util.Cp56Time2aUtil;
import com.xtjc.recsyslog.util.YunCrcUtil;
import lombok.extern.slf4j.Slf4j;

import java.nio.charset.StandardCharsets;
import java.util.Date;

@Slf4j
public class ChargeCommand {

    private static final String before = "68";

    /**
     * 读取实时监测数据
     *
     * @param gunNumber 枪号
     * @return 这个命令下发完    没达到预期效果     待沟通确认
     */
    public static String[] getChargeData(String gunNumber, ChargeEntity charge) {
        String[] number = charge.getSerialNumber();
        String encryptFlag = "00";
        String type = "12";//读取实时监测数据
        String[] sn = charge.getSn16();
        String[] body = ArrayUtil.newArray(String.class, 12);
        System.arraycopy(number, 0, body, 0, number.length);
        body[2] = encryptFlag;
        body[3] = type;
        System.arraycopy(sn, 0, body, 4, sn.length);
        body[11] = gunNumber;
        return commend(body);
    }

    /**
     * 充电桩工作参数设置
     *
     * @param workStatus 工作状态
     * @param output     输出电压功率百分之比
     * @return 这个命令下发完    没达到预期效果     待沟通确认
     */
    public static String[] setChargeWorkParam(boolean workStatus, int output, ChargeEntity charge) {
        String[] number = charge.getSerialNumber();
        String encryptFlag = "00";
        String type = "52";//充电桩工作参数设置
        String[] sn = charge.getSn16();
        String[] body = ArrayUtil.newArray(String.class, 13);
        System.arraycopy(number, 0, body, 0, number.length);
        body[2] = encryptFlag;
        body[3] = type;
        System.arraycopy(sn, 0, body, 4, sn.length);
        body[11] = workStatus ? "00" : "01";
        body[12] = ConverUtil.byteToHex(output);
        return commend(body);
    }

    /**
     * 对时设置
     *
     * @param charge
     */
    public static String[] setChargeTimeZone(ChargeEntity charge) {
        String[] number = charge.getSerialNumber();
        String encryptFlag = "00";
        String type = "56";//对时设置
        String[] sn = charge.getSn16();
        String[] time = Cp56Time2aUtil.date2HexArr(new Date());
        String[] body = ArrayUtil.newArray(String.class, 18);
        System.arraycopy(number, 0, body, 0, number.length);
        body[2] = encryptFlag;
        body[3] = type;
        System.arraycopy(sn, 0, body, 4, sn.length);
        System.arraycopy(time, 0, body, 11, time.length);
        return commend(body);
    }

    /**
     * 重启充电桩
     *
     * @param flag   立即重启   空闲重启
     * @param charge
     * @return 这个命令下发完    没达到预期效果     待沟通确认
     */
    public static String[] restart(boolean flag, ChargeEntity charge) {
        String[] number = charge.getSerialNumber();
        String encryptFlag = "00";
        String type = "92";//远程重启
        String[] sn = charge.getSn16();
        String[] body = ArrayUtil.newArray(String.class, 12);
        System.arraycopy(number, 0, body, 0, number.length);
        body[2] = encryptFlag;
        body[3] = type;
        System.arraycopy(sn, 0, body, 4, sn.length);
        body[11] = flag ? "01" : "02";
        return commend(body);
    }

    /**
     * 远程启动充电
     *
     * @param charge
     * @return
     */
    public static String[] remoteStartCharge(ChargeEntity charge) {
        String[] number = charge.getSerialNumber();
        String[] txnSeqNum = charge.getTxnSeqNum();
        String[] sn = charge.getSn16();
        String[] ljNum = {"00", "00", "00", "00", "00", "00", "00", "01"};
        String[] wlNum = {"00", "00", "00", "00", "00", "00", "00", "01"};
        String[] balance = {"00", "01","86","A0"};
        String encryptFlag = "00";
        String type = "34";//运营平台远程控制启机
        String[] body = ArrayUtil.newArray(String.class, 46);
        System.arraycopy(number, 0, body, 0, number.length);
        body[2] = encryptFlag;
        body[3] = type;
        System.arraycopy(txnSeqNum, 0, body, 4, txnSeqNum.length);
        System.arraycopy(sn, 0, body, 20, sn.length);
        body[27] = "01";//枪号
        System.arraycopy(ljNum, 0, body, 28, ljNum.length);
        System.arraycopy(wlNum, 0, body, 36, wlNum.length);
        System.arraycopy(balance, 0, body, 44, balance.length);
        return commend(body);
    }

    /**
     * 运营平台远程停机
     *
     * @param charge
     * @return
     */
    public static String[] remoteStopCharge(ChargeEntity charge) {
        String[] number = charge.getSerialNumber();
        String[] sn = charge.getSn16();
        String encryptFlag = "00";
        String gNum = "01";
        String type = "36";//运营平台远程停机
        String[] body = ArrayUtil.newArray(String.class, 12);
        System.arraycopy(number, 0, body, 0, number.length);
        body[2] = encryptFlag;
        body[3] = type;
        System.arraycopy(sn, 0, body, 4, sn.length);
        body[11] = gNum;
        return commend(body);
    }

    /**
     * 下发二维码前缀指令
     *
     * @param charge
     * @return
     */
    public static String[] sendQrCodeData(ChargeEntity charge) {
        String uri = "www.baidu.com?No=";
        String[] uri_Ascii = ConverUtil.bytesToHexArray(uri.getBytes(StandardCharsets.US_ASCII));
        String[] number = charge.getSerialNumber();
        String[] sn = charge.getSn16();
        String encryptFlag = "00";
        String type = "F0";//下发二维码前缀指令
        String[] body = ArrayUtil.newArray(String.class, 13 + uri_Ascii.length);
        System.arraycopy(number, 0, body, 0, number.length);
        body[2] = encryptFlag;
        body[3] = type;
        System.arraycopy(sn, 0, body, 4, sn.length);
        body[11] = "00";
        body[12] = ConverUtil.byteToHex(uri_Ascii.length);
        System.arraycopy(uri_Ascii, 0, body, 13, uri_Ascii.length);
        return commend(body);
    }


    private static String[] commend(String[] body) {
        byte[] crcData = YunCrcUtil.calculateCrc(ConverUtil.hexArrayToByteArray(body));
        byte lowByte = crcData[1];
        byte hiByte = crcData[0];
        int length = body.length + 4;
        String[] resAck = ArrayUtil.newArray(String.class, body.length + 4);
        resAck[0] = before;
        resAck[1] = String.format("%02X", body.length);
        System.arraycopy(body, 0, resAck, 2, body.length);
        resAck[length - 2] = ConverUtil.byteToHex(lowByte);
        resAck[length - 1] = ConverUtil.byteToHex(hiByte);
        return resAck;
    }

}
